package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.IsAdminEnum;
import com.member.common.enums.StandingEnum;
import com.member.shop.domain.SysBizUser;
import com.member.shop.service.ISysBizUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 【管理员列表】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-12-28
 **/
@RestController
@RequestMapping("/shop/user/admin")
public class SysBizUserAdminController extends BaseController {
    @Resource
    private ISysBizUserService sysBizUserService;

    /**
     * 查询管理员列表
     */
    @PreAuthorize("@ss.hasPermi('shop:userAdmin:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizUser sysBizUser) {
        startPage();
        List<SysBizUser> list = sysBizUserService.selectSysBizUserAdminList(sysBizUser);
        return getDataTable(list);
    }

    /**
     * 获取管理员列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:userAdmin:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizUserService.selectSysBizUserById(id));
    }

    /**
     * 新增管理员列表
     */
    @PreAuthorize("@ss.hasPermi('shop:userAdmin:add')")
    @Log(title = "管理员列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizUser sysBizUser) {

        LambdaQueryWrapper<SysBizUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysBizUser::getUserPhone, sysBizUser.getUserPhone());
        queryWrapper.eq(SysBizUser::getIsAdmin, IsAdminEnum.ADMINISTRATOR.getKey());
        List<SysBizUser> sysBizUsers = sysBizUserService.selectSysBizUserList(queryWrapper);
        if (!sysBizUsers.isEmpty()) {
            return warn("该用户手机号已经是管理员");
        }else{
            if(sysBizUserService.hasUserPhone(sysBizUser.getUserPhone())){
                LambdaQueryWrapper<SysBizUser> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(SysBizUser::getUserPhone, sysBizUser.getUserPhone());
                List<SysBizUser> sysBizUsers1 = sysBizUserService.selectSysBizUserList(queryWrapper1);
                return AjaxResult.exist(sysBizUsers1.get(0).getId());
            }
        }
        sysBizUser.setIsAdmin(IsAdminEnum.ADMINISTRATOR.getKey());
        return toAjax(sysBizUserService.insertSysBizUser(sysBizUser));
    }

    /**
     * 修改管理员列表
     */
    @PreAuthorize("@ss.hasPermi('shop:userAdmin:edit')")
    @Log(title = "管理员列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizUser sysBizUser) {
        sysBizUser.setIsAdmin(sysBizUser.getIsAdmin());
        sysBizUser.setIsAdminList(1);
        return toAjax(sysBizUserService.updateSysBizUserAdmin(sysBizUser));
    }

    /**
     * 修改管理员列表
     */
    @PreAuthorize("@ss.hasPermi('shop:userAdmin:edit')")
    @Log(title = "管理员列表", businessType = BusinessType.UPDATE)
    @PutMapping("/updateIsAdmin")
    public AjaxResult updateIsAdmin(@RequestBody SysBizUser sysBizUser) {
        return toAjax(sysBizUserService.update(new LambdaUpdateWrapper<SysBizUser>().set(SysBizUser::getIsAdmin, sysBizUser.getIsAdmin()).eq(SysBizUser::getId, sysBizUser.getId())));
    }
}
